#include "logview.h"
#include "ui_logview.h"

LogView::LogView(QWidget* parent)
    : QWidget(parent),
    ui(new Ui::LogView()),
    m_trace_widget(std::make_shared<QTextBrowser>()),
    m_info_widget(std::make_shared<QTextBrowser>()),
    m_warning_widget(std::make_shared<QTextBrowser>()),
    m_exception_widget(std::make_shared<QTextBrowser>()),
    m_error_widget(std::make_shared<QTextBrowser>())
{
    ui->setupUi(this);

    ui->tabWidget->removeTab(0);
    ui->tabWidget->removeTab(0);

    ui->tabWidget->addTab(m_trace_widget.get(), QIcon(":/icons/icons/debug.png"), "DEBUG");
    m_level_bind_tab.insert(MyLogLevel::DEBUG_, m_trace_widget);
    m_trace_widget->setWordWrapMode(QTextOption::NoWrap);
    m_trace_widget->setTextColor(QColor(0, 0, 0));

    ui->tabWidget->addTab(m_info_widget.get(), QIcon(":/icons/icons/info.png"), "INFO");
    m_level_bind_tab.insert(MyLogLevel::INFO_, m_info_widget);
    m_info_widget->setWordWrapMode(QTextOption::NoWrap);
    m_info_widget->setTextColor(QColor(0, 0, 0));

    ui->tabWidget->addTab(m_warning_widget.get(), QIcon(":/icons/icons/warning.png"), "WARNING");
    m_level_bind_tab.insert(MyLogLevel::WARNING_, m_warning_widget);
    m_warning_widget->setWordWrapMode(QTextOption::NoWrap);
    m_warning_widget->setTextColor(QColor(255, 150, 0));

    ui->tabWidget->addTab(m_exception_widget.get(), QIcon(":/icons/icons/exception.png"), "EXCEPTION");
    m_level_bind_tab.insert(MyLogLevel::EXCEPTION_, m_exception_widget);
    m_exception_widget->setWordWrapMode(QTextOption::NoWrap);
    m_exception_widget->setTextColor(QColor(0, 180, 255));

    ui->tabWidget->addTab(m_error_widget.get(), QIcon(":/icons/icons/error.png"), "ERROR");
    m_level_bind_tab.insert(MyLogLevel::ERROR_, m_error_widget);
    m_error_widget->setWordWrapMode(QTextOption::NoWrap);
    m_error_widget->setTextColor(QColor(255, 0, 0));

    connect(MyLogger.get(), &V_PDT::AsyncLog::print, this, &LogView::printLog);
}

LogView::~LogView()
{
    delete ui;
}

void LogView::printLog(MyLogLevel level, const QString& loginfo)
{
    m_level_bind_tab[level]->append(loginfo);
}

